var app = new Vue({
    el: '.box',
    data: {
        tableData: '',//消息数据
        dialogFormVisible: false,//显示编辑界面
        dialogFormVisible1: false,//显示新建消息
        formLabelWidth: '70px',//宽度
        form: '',//编辑内容
        form1: {
            conts: '',
            url: '',
            flag: true,
            timer: '',
            head: 1011,
            token: (sessionStorage.getItem('token'))
        },//新建内容
    },
    methods: {
        save(obj, type) {
            if (type) {
                this.dialogFormVisible = !this.dialogFormVisible;
                this.form = this.tableData[obj.$index];
            } else {
                this.dialogFormVisible = false;
                this.form.flag = this.form.flag1 ? 1 : 0;
                this.form.head = 1010;
                this.form.token = (sessionStorage.getItem('token'));
                save_msg(this.form);
            }
            // 保存编辑
        },
        new_msgs() {
            this.dialogFormVisible1 = false;
            new_mag(this.form1);
            // 新建消息
        },
        del(obj) {
            var arr = {};
            arr.ids = this.tableData[obj.$index].Id;
            arr.head = 1012;
            arr.token = (sessionStorage.getItem('token'));
            this.$confirm('此操作将永久删除该消息, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                del_msg(arr);
            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
            //删除消息
        },
    }
});


function del_msg(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",
        baseURL: '../../api/msg.php',
        data: 'data=' + JSON.stringify(obj)
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.$message({
                message: '操作成功~',
                type: 'success'
            });
            select_msg();
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);
    });
};//删除消息

function save_msg(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",
        baseURL: '../../api/msg.php',
        data: 'data=' + JSON.stringify(obj)
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.$message({
                message: '操作成功~',
                type: 'success'
            });
            select_msg();
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);
    });
};//保存消息

function new_mag(obj) {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",
        baseURL: '../../api/msg.php',
        data: 'data=' + JSON.stringify(obj)
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            app.$message({
                message: '操作成功~',
                type: 'success'
            });
            select_msg();
        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);
    });
};//新建消息

select_msg();
function select_msg() {
    var loading = app.$loading({
        lock: true,
        text: 'Loading',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'
    });
    axios({
        method: "POST",//类型
        baseURL: '../../api/msg.php',//地址
        data: 'data=' + JSON.stringify({ head: 1009, token: (sessionStorage.getItem('token')) })
    }).then(function (res) {
        loading.close();
        if (res.data.code === '200') {
            for (var i = 0; i < res.data.data.length; i++) {
                res.data.data[i].flag1 = (res.data.data[i].flag - 0) === 1 ? true : false;
                res.data.data[i].flag2 = (res.data.data[i].flag - 0) === 1 ? true : false;
                res.data.data[i].ids = res.data.data[i].Id;
            };
            app.tableData = res.data.data;


        } else if (res.data.code === '500') {
            window.location.href = res.data.href;
        } else {
            console.log(res);
        };
    }).catch(function (err) {
        console.log(err);//失败返回错误信息
    });
};//获取消息